package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.CheckGroupDao;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.pojo.CheckGroup;
import com.itcast.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组，同时需要让检查组关联检查项
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组
        checkGroupDao.add(checkGroup);

        //设置检查组和检查项之间的多对多关联关系，操作t_checkitem_checkgroup表
        Integer checkGroupId = checkGroup.getId();

        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);

    }

//    分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();

        Integer pageSize = queryPageBean.getPageSize();

        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<CheckGroup> page=checkGroupDao.findByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询检查项回显数据
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //编辑修改检查组
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改checkgroup信息
        checkGroupDao.edit(checkGroup);
        //将原有的关联数据进行删除
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //建立新的链接
        //设置检查组和检查项之间的多对多关联关系，操作t_checkitem_checkgroup表
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);

    }

    //根据id删除检查组
    @Override
    public void deleteById(Integer id) {
        long checkgroupId=checkGroupDao.findCountCheckGroupId(id);
        if (checkgroupId>0){
            new RuntimeException();
        }
        checkGroupDao.deleteById(id);
    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupDao.findAll();
    }

    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if (checkitemIds!= null && checkitemIds.length > 0){
            for (Integer checkItemId : checkitemIds) {
                Map<String,Integer> map= new HashMap();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

}
